// pages/Cart/index.js
Page({
    data: {
        uid: null,
        list: [],
        commodity: [],
        allCheck: false,
        all_money: 0,
        all: false
    },
    checked: [],
    uid: null,
    onLoad: function (options) {

        wx.getStorage({
            key: "user",
            success: (res) => {
                let uid = null;
                uid = res.data.uid;
                this.setData({
                    uid
                });

                if (uid != null)
                    wx.request({
                        url: 'http://460d80b632.zicp.vip/WeChat/cart.php',
                        method: "POST",
                        header: {
                            "Content-Type": "application/x-www-form-urlencoded"
                        },
                        data: {
                            "uid": uid
                        },
                        success: res => {
                            let list = res.data;
                            this.setData({
                                list
                            });
                        }
                    });

                let list = new Array();
                list = this.data.list;
                for (let c = 0; c < list.length; c++) {
                    const item = list[c];
                    this.checked[item.scid] = false;
                }

            }
        });

        wx.request({
            url: 'http://460d80b632.zicp.vip/WeChat/commodity.php',
            method: "POST",
            header: {
                "Content-Type": "application/x-www-form-urlencoded"
            },
            data: {
                "ssid": 0
            },
            success: res => {
                let commodity = res.data;
                this.setData({
                    commodity
                });
            }
        });

    },
    buttonChange: function (res) {
        let dataset = res.currentTarget.dataset;
        let scid = Number(dataset.id);
        let operation = Number(dataset.operation);

        let list = new Array();
        list = this.data.list;

        let c;
        for (c = 0; c < list.length; c++) {
            const item = list[c];
            if (Number(list[c].scid) == scid)
                break;
        }

        if (Number(list[c].sc_num) == 1 && operation == -1)
            return;
        list[c].sc_num = Number(list[c].sc_num) + operation;
        this.setData({
            list
        });
        wx.request({
            url: 'http://460d80b632.zicp.vip/WeChat/updataCart.php',
            method: "POST",
            header: {
                "Content-Type": "application/x-www-form-urlencoded"
            },
            data: {
                "scid": list[c].scid,
                "sc_num": list[c].sc_num
            },
            success: res => {
                if (!res.success) {
                    wx.showToast({
                        title: res.msg,
                        duration: 0,
                        icon: "error",
                        mask: true,
                        success: (res) => {},
                        fail: (res) => {},
                        complete: (res) => {},
                    })
                }
            }
        });

        if (this.checked[scid]) {
            let all_money = this.data.all_money;
            if (operation == 1) {
                all_money += Number(list[c].c_price);
            } else {
                all_money -= Number(list[c].c_price);
            }
            this.setData({
                all_money
            });
        }

    },
    form: function (res) {
        let list = new Array();
        list = this.data.list;
        let array = new Array();
        wx.request({
            url: 'http://460d80b632.zicp.vip/WeChat/buy.php',
            method: "POST",
            header: {
                "Content-Type": "application/x-www-form-urlencoded"
            },
            data: {
                "ssid": res.detail.value.all_checkbox
            },
            success: res => {
                wx.request({
                    url: 'http://460d80b632.zicp.vip/WeChat/cart.php',
                    method: "POST",
                    header: {
                        "Content-Type": "application/x-www-form-urlencoded"
                    },
                    data: {
                        "uid": this.data.uid
                    },
                    success: res => {
                        let list = res.data;
                        this.setData({
                            list,
                            allCheck: false,
                            all_money: 0,
                            all: false
                        });
                    }
                });
            }
        })
    },
    cartItem: function (res) {
        let value = new Array();
        value = res.detail.value;

        this.setData({
            all: false
        })

        let list = new Array();
        list = this.data.list;
        for (let c = 0; c < list.length; c++) {
            const item = list[c];
            this.checked[item.scid] = false;
        }

        let all_money = 0;
        for (let c = 0; c < value.length; c++) {
            const item = value[c];
            let pos = 0;
            for (pos = 0; pos < list.length; pos++) {
                const items = list[pos];
                if (item == items.scid) {
                    all_money += items.c_price * items.sc_num;
                    this.checked[item] = true;
                    break;
                }
            }
        }
        this.setData({
            all_money
        });
    },
    cartAll: function (res) {
        let value = new Array();
        value = res.detail.value;
        let all_money = 0;
        let list = new Array();
        list = this.data.list;
        if (value.length != 0) {
            for (let c = 0; c < list.length; c++) {
                const item = list[c];
                all_money += item.c_price * item.sc_num;
                this.checked[item.scid] = true;
            }

            this.setData({
                all: true,
                allCheck: true
            });
        } else {
            this.setData({
                all: false,
                allCheck: false
            });
            for (let c = 0; c < list.length; c++) {
                const item = list[c];
                this.checked[item.scid] = false;
            }
        }
        this.setData({
            all_money
        });
    }
})